package com.pioneer.controller.train;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.train.domain.TrainCategory;
import com.pioneer.web.train.service.ITrainCategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * Title:TrainCategoryController
 * Description:
 *
 * @Author 罗熠轩
 * @Date 25/2/2025 AM10:18
 * @Version 1.0
 **/


@RestController
@RequestMapping("/train/category")
public class TrainCategoryController extends BaseController {

    @Resource
    private ITrainCategoryService categoryService;

    /**
     * 获取培训类别列表
     *
     * @param category 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('train:category:list')")
    @GetMapping("/list")
    public AjaxResult list(TrainCategory category) {
        boolean isPage = startPage();
        List<TrainCategory> list = categoryService.list(Wrappers.query(category));
        return isPage ? getDataTable(list) : AjaxResult.success(list);
    }

    /**
     * 根据培训类别id获取详细信息
     *
     * @param categoryId 培训类别id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('train:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable Long categoryId) {
        return AjaxResult.success(categoryService.getById(categoryId));
    }

    /**
     * 新增培训类别
     *
     * @param category 培训类别
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('train:category:add')")
    @Log(title = "培训类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TrainCategory category) {
        category.setCreateBy(getUsername());
        return toAjax(categoryService.save(category));
    }

    /**
     * 修改培训类别
     *
     * @param category 培训类别
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('train:category:edit')")
    @Log(title = "培训类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TrainCategory category) {
        category.setUpdateBy(getUsername());
        return toAjax(categoryService.updateById(category));
    }

    /**
     * 删除培训类别
     *
     * @param categoryIds 培训类别id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('train:category:remove')")
    @Log(title = "培训类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(categoryService.removeByIds(Arrays.asList(categoryIds)));
    }
}
